<?php
$isReloadable = true;
require_once("../../pers/inc.config.php");
require_once(R2_FS_WEB_ROOT.'/app/_inc/inc.session.php');
require_once(R2_FS_WEB_ROOT.'/app/_inc/funct.html.php');

if (rint2Vx($objSess->aSV,'visione_att','vai_a_data')=='') {
	$objSess->aSV['visione_att']['vai_a_data'] = date('d/m/Y');
}
if (rint2Vx($objSess->aSV,'visione_att','ass_corr')=='') {
	$objSess->aSV['visione_att']['ass_corr'] = 'N';
}
if (reqVar('ass_corr')!='') {
	$objSess->aSV['visione_att']['ass_corr'] = reqVar('ass_corr');
}
$_REQUEST['ass_corr'] = rint2Vx($objSess->aSV,'visione_att','ass_corr');

$odx = new rint2Date(rint2Vx($objSess->aSV,'visione_att','vai_a_data'),'dmy');

switch (reqVar('azione')) {
	case 'salva':
		$msgPag .= confAtt();
		break;
	case 'go':
		if ($odx->setDMY(reqVar('vai_a_data'))) {
			$objSess->aSV['visione_att']['vai_a_data'] = $odx->getDMY();
		} else {
			$msgPag = "data ".reqVar('vai_a_data')." errata <br>";
		}	
		$odx->setDMY(rint2Vx($objSess->aSV,'visione_att','vai_a_data'));
		break;
	case 'suc':
		$odx->setDayAdd(7);
		$objSess->aSV['visione_att']['vai_a_data'] = $odx->getDMY();
		break;
	case 'pre':
		$odx->setDayAdd(-7);
		$objSess->aSV['visione_att']['vai_a_data'] = $odx->getDMY();
		break;
}
$_REQUEST['vai_a_data'] = $odx->getDMY();

$utsLun = $odx->weekFirst('unix');
$utsDom = $odx->getDayAdd(6,'unix',$utsLun,'unix');

// lunedi\' settimana in oggetto.
$ymdLun = $odx->getYMD('-',4,$utsLun,'unix');
// domenica settimana in oggetto.
$ymdDom = $odx->getYMD('-',4,$utsDom,'unix');
//dbg_("$ymdLun - $ymdDom")
// array modalita' visualizzazione sorgenti.
$aVisAss['S'] = "Assegnazioni correnti";
$aVisAss['N'] = "Tutte le assegnazioni";

$aRdOpt = array();
$aRdOpt['onClick'] = "javascript:submAzione('');";

// 1 - attivita' teoriche no-rint. 
$sSql = " SELECT c.id_comm, c.cod_comm, c.descr as c_descr, c.data_iniz, c.data_fine, ".
		 " a.tipo_att, a.flag_corrente, ap.tipo_rilevazione ".
		 " FROM commessa c ".
		 " INNER JOIN att_previste ap ON ap.id_comm = c.id_comm ". 
		 "                           AND ap.id_rint = 0 AND ap.tipo_rilevazione IN ('A','X') ".
		 " INNER JOIN assegnaz a ON a.id_comm = c.id_comm ".
		 "                       AND a.id_rint = 0 ".
		 "                       AND a.tipo_att = ap.tipo_att ".
		 "                       AND a.id_operatore = ".$objSess->idUtente.		
		 " WHERE c.data_iniz <= '".$ymdDom."' OR c.data_fine >= '".$ymdLun."' ".
         " ORDER BY c_descr, ap.tipo_att";
$objDB->query($sSql,'sAttA');
//dbg_($sSql);

$aAtt = array();
while($aRecX = $objDB->fetch('sAttA')) {
	if (reqVar('ass_corr') == 'S' && $aRecX['flag_corrente'] == 'N') {continue;}
	$aAtt[$aRecX['id_comm']]['desCom'] = $aRecX['cod_comm']." - ". $aRecX['c_descr'];
	$aAtt[$aRecX['id_comm']]['diCom']  = $aRecX['data_iniz'];
	$aAtt[$aRecX['id_comm']]['dfCom']  = $aRecX['data_fine'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['desAtt']  = $aTipiAttRint[$aRecX['tipo_att']]['des'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['flag']    = $aRecX['flag_corrente'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['tipoRil'] = $aRecX['tipo_rilevazione'];
}
//dbg_("aAtt - 1 : ".print_r($aAtt,1));

// 2 - attivita' gia' registrate - no-rint.
$sSql = " SELECT c.id_comm, c.cod_comm, c.descr as c_descr, c.data_iniz, c.data_fine, ". 
		 " at.tipo_att, IFNULL(a.flag_corrente, 'N') AS flag_corrente, ap.tipo_rilevazione, ".
         " IFNULL(at.ore, 0.0) as at_ore, IFNULL(at.data, '0000-00-00') as at_data ". 
		 " FROM attivita at ".
 		 " INNER JOIN commessa c ON c.id_comm = at.id_comm ".
		 " LEFT JOIN att_previste ap ON ap.id_comm = at.id_comm ". 
		 "                         AND ap.id_rint = 0 ".
		 "                         AND ap.tipo_att = at.tipo_att ".	     
		 " LEFT JOIN assegnaz a ON a.id_comm = at.id_comm ".
		 "                     AND a.id_rint = 0 ". 
		 "                     AND a.tipo_att = at.tipo_att ".
         "                     AND a.id_operatore = at.id_operatore ".		
		 " WHERE at.id_operatore = ".$objSess->idUtente.
		 " AND at.data BETWEEN '".$ymdLun."' AND '".$ymdDom."' ".
		 " AND at.id_rint = 0 ";
$objDB->query($sSql,'sAttB');
//dbg_($sSql);

while($aRecX = $objDB->fetch('sAttB')) {
	if ($aRecX['at_ore'] == 0 && reqVar('ass_corr') == 'S' && $aRecX['flag_corrente'] == 'N') {continue;}
	$aAtt[$aRecX['id_comm']]['desCom'] = $aRecX['cod_comm']." - ". $aRecX['c_descr'];
	$aAtt[$aRecX['id_comm']]['diCom']  = $aRecX['data_iniz'];
	$aAtt[$aRecX['id_comm']]['dfCom']  = $aRecX['data_fine'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['desAtt']  = $aTipiAttRint[$aRecX['tipo_att']]['des'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['flag'] = $aRecX['flag_corrente'];
	$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']]['tipoRil'] = $aRecX['tipo_rilevazione'];
	if (isset($aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']][$aRecX['at_data']])) {
		$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']][$aRecX['at_data']] += $aRecX['at_ore'];
	} else {
		$aAtt[$aRecX['id_comm']]['rint'][0]['att'][$aRecX['tipo_att']][$aRecX['at_data']]  = $aRecX['at_ore'];
	}
	
	if (isset($aAtt['totOreData'][$aRecX['at_data']])) {
		$aAtt['totOreData'][$aRecX['at_data']] += $aRecX['at_ore'];
	} else {
		$aAtt['totOreData'][$aRecX['at_data']]  = $aRecX['at_ore'];
	}
}
//dbg_("aAtt - 2 : ".print_r($aAtt,1));

// 3 - attivita' teoriche SI-rint. 
$sSql = " SELECT c.id_comm, c.cod_comm, c.descr as c_descr, c.data_iniz, c.data_fine ". 
		 ", a.tipo_att, r.id_rint, a.flag_corrente, ap.tipo_rilevazione ".
		 ", r.descr AS r_descr, r.data_ora_chiusura, r.data_ora_ins ".
		 " FROM rint r ".
		 " INNER JOIN commessa c ON c.id_comm = r.id_comm ".
		 " INNER JOIN assegnaz a ON a.id_rint = r.id_rint ".
		 "                       AND a.id_operatore = ".$objSess->idUtente.		
		 " LEFT JOIN att_previste ap ON ap.id_comm = c.id_comm ". 
		 "                         AND ap.id_rint = 0 ".
		 "                         AND ap.tipo_att = a.tipo_att ".	     
		 " WHERE r.data_ora_chiusura < '2000-01-01 00:00:00' OR r.data_ora_chiusura >= '".$ymdLun." 23:59:59' ".
		 " ORDER BY c_descr, a.tipo_att";
$objDB->query($sSql,'sAttA');
//dbg_($sSql);

while($aRecX = $objDB->fetch('sAttA')) {
	//dbg_($aRecX);
	if (reqVar('ass_corr') == 'S' && $aRecX['flag_corrente'] == 'N') {continue;}
	$aAtt[$aRecX['id_comm']]['desCom'] = $aRecX['cod_comm']." - ". $aRecX['c_descr'];
	$aAtt[$aRecX['id_comm']]['diCom']  = $aRecX['data_iniz'];
	$aAtt[$aRecX['id_comm']]['dfCom']  = $aRecX['data_fine'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['des'] = $aRecX['r_descr'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['dataChiu'] = substr($aRecX['data_ora_chiusura'],0,10);
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['dataIns'] = substr($aRecX['data_ora_ins'],0,10);
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['desAtt']  = $aTipiAttRint[$aRecX['tipo_att']]['des'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['flag']    = $aRecX['flag_corrente'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['tipoRil'] = $aRecX['tipo_rilevazione'];
}
//dbg_("aAtt - 3 : ".print_r($aAtt,1));

// 4 - attivita' gia' registrate - SI-rint.
$sSql = " SELECT c.id_comm, c.cod_comm, c.descr as c_descr, c.data_iniz, c.data_fine ". 
		 ", at.tipo_att, r.id_rint, IFNULL(a.flag_corrente, 'N') AS flag_corrente, ap.tipo_rilevazione ".
       ", at.ore AS at_ore, at.data AS at_data ". 
		 ", r.descr AS r_descr, r.data_ora_chiusura, r.data_ora_ins ".
		 " FROM rint r ".
		 " INNER JOIN commessa c ON c.id_comm = r.id_comm ".
		 " INNER JOIN attivita at ON at.id_rint = r.id_rint ".
		 "                          AND at.id_operatore = ".$objSess->idUtente.
		 "    AND at.data BETWEEN '$ymdLun' AND '$ymdDom' ".
		 " LEFT JOIN assegnaz a ON a.id_rint = r.id_rint ". 
		 "                     AND a.tipo_att = at.tipo_att ".
       "                     AND a.id_operatore = at.id_operatore ".		
		 " LEFT JOIN att_previste ap ON ap.id_comm = c.id_comm ". 
		 "                         AND ap.id_rint = 0 ".
		 "                         AND ap.tipo_att = a.tipo_att ";     
$objDB->query($sSql,'sAttB');
//dbg_($sSql);

while($aRecX = $objDB->fetch('sAttB')) {
	if ($aRecX['at_ore'] == 0 && reqVar('ass_corr') == 'S' && $aRecX['flag_corrente'] == 'N') {
			continue;
	}
	//dbg_($aRecX['cod_comm']."/".$aRecX['id_rint']."/".$aRecX['tipo_att']."/".$aRecX['at_data'].":".$aRecX['at_ore']);
	$aAtt[$aRecX['id_comm']]['desCom'] = $aRecX['cod_comm']." - ". $aRecX['c_descr'];
	$aAtt[$aRecX['id_comm']]['diCom']  = $aRecX['data_iniz'];
	$aAtt[$aRecX['id_comm']]['dfCom']  = $aRecX['data_fine'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['des'] = $aRecX['r_descr'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['dataChiu'] = substr($aRecX['data_ora_chiusura'],0,10);
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['dataIns'] = substr($aRecX['data_ora_ins'],0,10);
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['desAtt']  = $aTipiAttRint[$aRecX['tipo_att']]['des'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['flag'] = $aRecX['flag_corrente'];
	$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']]['tipoRil'] = $aRecX['tipo_rilevazione'];	
	if (isset($aAtt[$aRecX['id_comm']][$aRecX['id_rint']]['att'][$aRecX['tipo_att']][$aRecX['at_data']])) {
		$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']][$aRecX['at_data']] += $aRecX['at_ore'];
	} else {
		$aAtt[$aRecX['id_comm']]['rint'][$aRecX['id_rint']]['att'][$aRecX['tipo_att']][$aRecX['at_data']]  = $aRecX['at_ore'];
	}
	if (isset($aAtt['totOreData'][$aRecX['at_data']])) {
		$aAtt['totOreData'][$aRecX['at_data']] += $aRecX['at_ore'];
	} else {
		$aAtt['totOreData'][$aRecX['at_data']]  = $aRecX['at_ore'];
	}
}
//dbg_($aAtt);
$cmdL = "";
$cmdL .= "<a class='cmd1' href=\"attIns.php?data=".urlencode(rint2Vx($objSess->aSV,'visione_att','vai_a_data'))
	."&xsn=".XSN."\">inserisci attivita</a>";
$cmdL .= "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;";
//$cmdL .= "<a class='cmd1' href=\"attSint.php?xsn=".XSN."\">visione sintetica</a>";
	
$cmdR  = "";
$cmdR .= "<a class='cmd1' href='#' onclick=\"submAzione('salva');\">conferma</a>";
$cmdR .= cmdTorna('', 3); 

$sPaginaz  = "";
$sPaginaz .= "<a class='cmd1' href='#' onclick=\"submAzione('pre')\">sett. prec.</a>";
$sPaginaz .= "&nbsp;&nbsp;&nbsp;";
$sPaginaz .= "<a class='cmd1' href='#' onclick=\"submAzione('suc')\">sett. succ.</a>";

$aMenuPag = array();
include("./_inc/inc.aMenuPag.php");
testaHtml('Imputazione Attivita\' - visione dettagliata', $msgPag, '30.10.10', $aMenuPag);

echo getHtmlForm();
echo getHtmlHidden('azione','...');
echo getHtmlHidden('xsn',XSN);
?>
<script>
function navWeek(str) {
	document.frm1.week.value=str;
	document.frm1.azione.value='';
	document.frm1.submit();
}

function submAzione(azione) {
	document.frm1.azione.value=azione;
	document.frm1.submit();
}

</script>

<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
      		<?=$cmdL?>
      </td>
      <td class="cmd1" align="left">
      		&nbsp;
      </td>
      <td class="cmd1" align="right">
      		<?=$cmdR?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">Vai alla data&nbsp;:&nbsp;
			<?
			echo getHtmlDate('vai_a_data'); ?>&nbsp;&nbsp;
	  		<a class='cmd1' href='#' onclick='submAzione("go");'>vai</a>
      </td>
      <td class="cmd1" align="center">
			<?=getHtmlRadio('ass_corr', $aVisAss, '@REQ', $aRdOpt) ?>
      </td>
      <td class="cmd1" align="right">
			<?=$sPaginaz?>
      </td>
   </tr>
</table>

<table class="tab1" border="0" cellpadding="1" cellspacing="1" width="100%" align='center'>
	<tr>
		<td class='lab1' align='center' width='4%'>&nbsp;</td>
		<td class='lab1' align='center' width='40%' >Commessa / Rint / Attivita'</td>
<?	
for($k=0;$k<7;$k++) {		
	echo "<td class='lab1' align='center' with='8%'>";
	$odx->setDayAdd($k,'unix',$utsLun,'unix');
	if ($odx->isHoliday()) {
		$sStyle = 'color:#ff0000;';
		$oreLav = 0;
	} else {
		$sStyle = '';
		$oreLav = 7.5;
	}	
	echo "<a class='lab1' href=\"attIns.php?data=".urlencode($odx->getDMY())
		."&xsn=".XSN."\" title=\"inserisci attivita'\" style=\"$sStyle\">"
		.$odx->weekDay('it-long')."</br>".$odx->getDMY()."</a>";
	echo "</td>";
}		
?>
	</tr>
	<tr>
		<td class='lab1' align='right' colspan='2' height='25'><b>Ore mancanti :&nbsp;</b></td>
<? 
	for($k=0;$k<7;$k++) {
		$odx->setDayAdd($k,'unix',$utsLun,'unix');
		$oreLav = ($odx->isHoliday()) ? 0 : 7.5;
		$ymdX = $odx->getYMD();
		$oreManc = (isset($aAtt['totOreData'][$ymdX])) ? $oreLav - $aAtt['totOreData'][$ymdX] : $oreLav;
		if ($oreManc== 0) {
			$sStyle = "";
		} elseif($oreManc > 0) {
			$sStyle = "style='color:red;font-weight:bold;'";
		} else {
			$sStyle = "style='font-weight:bold;'";
		}
		echo "<td class='lab1' align='center' $sStyle>";
		echo num2str($oreManc, 1);	
		echo "</td>";		
	}
?>		

	</tr>
<?  
$nChk = 0;
foreach ($aAtt as $co => $aCo) {
	if ($co == 'totOreData') {continue;}
	$diC = $aCo['diCom'];	
	$dfC = $aCo['dfCom'];		
	echo "<tr>\n";
	echo "<td class='txt2' height='25' colspan='9' style='font-weight:bold;'>&nbsp;".$aCo['desCom'];
	if ($aCo['dfCom']<$ymdLun) {
		echo " (chiuso il ".YMD2DMY($aCo['dfCom']).")";		
	}
	echo "</td>\n";
	echo "</tr>";
	foreach ($aCo['rint'] as $idRintX => $aRintX) {
		if ($idRintX > 0) {
			$sSql = "SELECT SUM(ore) AS tot_ore FROM attivita WHERE id_rint = ".$idRintX." GROUP BY id_rint";
			$objDB->query($sSql);
			$acX = $objDB->fetch();
			$oreLav = (float)$acX['tot_ore'];
			$sSql = "SELECT SUM(stima) AS tot_ore FROM att_previste WHERE id_rint = ".$idRintX." GROUP BY id_rint";
			$objDB->query($sSql);
			$acX = $objDB->fetch();
			$oreStima = (float)$acX['tot_ore'];	
			
			echo "<tr>\n";
			echo "<td class='txt2' height='25' width='3%' style='font-weight:bold;text-align:right;'>"
				."<a href=\"".R2_URL_ROOT."/app/rint/rintView.php?id=$idRintX&xsn=".XSN."\" class=\"txt2\">"
				.$idRintX."</a>&nbsp;</td>\n";		
			echo "<td class='txt2' height='25' width='65%' colspan='4' style='font-weight:bold;'>"
				.substr($aRintX['des'],0,70)."</td>";
			echo "<td class='txt2' width='8%' style='font-weight:bold;text-align:center;'>".YMD2DMY($aRintX['dataIns'])."</td>\n";
			echo "<td class='txt2' width='8%' style='font-weight:bold;text-align:center;'>";
			if ($aRintX['dataChiu'] > '2001-01-01 00:00:00') {
				echo YMD2DMY($aRintX['dataChiu']);		
			} else {
				echo 'aperto';
			}
			echo "</td>\n";
			echo "<td class='txt2' width='8%' style='font-weight:bold;text-align:center'>stimate: ".$oreStima."</td>\n";
			echo "<td class='txt2' width='8%' style='font-weight:bold;text-align:center'>fatte: ".$oreLav."</td>\n";
			echo "</tr>\n";
		}
		foreach($aRintX['att'] as $ta => $aTiAt) {
?> 
	<tr>
		<td class='txt1' align='center'>
			<?		
			$suffReq = $co."_".$idRintX."_".$ta;	
			reqDef('zzchk_'.$suffReq, $aTiAt['flag']);
			echo ($idRintX == 0) ? getHtmlCheckBox('zzchk_'.$suffReq, 'S'):""; 
			echo getHtmlHidden('zzhid_'.$suffReq, 'X')
			?>
		</td>
		<td class='txt1'>
			<?
			echo ($idRintX > 0) ? (getHtmlCheckBox('zzchk_'.$suffReq, 'S')."&nbsp;&nbsp;"):""; 
			echo $aTiAt['desAtt'];
			?>
		</td>
<?	
			for($k=0;$k<7;$k++) {
				$odx->setDayAdd($k,'unix',$utsLun,'unix');
				$dt = $odx->getYMD();
				$dtUnix = $odx->getUnix();
				echo "<td class='txt1' align='center'>";	
				$ore = (isset($aTiAt[$dt])) ? $aTiAt[$dt] : 0;
				$bMod = true;
				if ($dt < $aCo['diCom']) { $bMod = false; }
				if ($dt > $aCo['dfCom']) { $bMod = false; }
				if ($idRintX > 0 && $aRintX['dataChiu'] > 2001-01-01 && $dt > $aRintX['dataChiu']) { $bMod = false; }
				if ($ore > 0) { $bMod = true; }
				if ($dt <= R2_DATA_CONSOLID) { $bMod = false; }
				if ($bMod) {
					reqDef('zzore_'.$suffReq."_".$dtUnix,($ore==0)?'':num2str($ore,1));
					//dbg_('zzore_'.$suffReq."_".$dtUnix."=".$ore);
					if ($odx->isHoliday()) {
						$aOptOre = array('style'=>'background-color:#ffdddd;');
					} else {
						$aOptOre = array('style'=>'background-color:#ffffff;');
					}
					echo getHtmlTextNum('zzore_'.$suffReq."_".$dtUnix,($k<5)?3:1,8,'@REQ',$aOptOre);		 
				} else {
					echo ($ore==0)?'':num2str($ore,1);
				}
				echo "</td>";		
			} 
			$nChk++;
		}
?>		
	</tr>
<?	
	}
}
?>
	<tr>
		<td class='lab1' align='right' colspan='2' height='25'><b>Tot. ore giorno :&nbsp;</b></td>
<?  
for($k=0;$k<7;$k++) {
	$odx->setDayAdd($k,'unix',$utsLun,'unix');
	$dt = $odx->getYMD();
	echo "<td class='lab1' align='center'><b>";
	if (isset($aAtt['totOreData'][$dt])) {
		echo num2str($aAtt['totOreData'][$dt], 1);	
	} else {
		echo '0,0';
	}
	echo "</b></td>";		
}
?>
	</tr>
</table>

<table class="cmd1" border="0" cellpadding="3" cellspacing="1" width="100%" align='center'>
   <tr>
      <td class="cmd1" align="left">
      		(r) = attivita' da rint
      </td>
      <td class="cmd1" align="right">
	  		<?=$sPaginaz?>
      </td>
   </tr>
   <tr>
      <td class="cmd1" align="left">
         	<?=$cmdL?>
      </td>
      <td class="cmd1" align="right">
         	<?=$cmdR?>
      </td>
   </tr>
</table>
<?
echo "</form>";

codaHtml();
exit();

function confAtt() {
	global $objDB, $objSess;
	$msg = '';
	$id_operatore = $objSess->idUtente;
	// aggiornamento tabella assegnaz rint = 0;
	foreach ($_REQUEST as $key => $value) {
		if (substr($key, 0, 6) != 'zzhid_') {continue;}		
		list($pref,$id_comm,$id_rint,$tipo_att)  = explode("_",$key); 
		if (reqVar('zzchk_'.substr($key, 6))=='S') {
			$flag_corrente = 'S'; 
		} else {
			$flag_corrente = 'N'; 
		}	
		
		$sSql = " UPDATE assegnaz SET ".
				   " flag_corrente = '".$flag_corrente."' ".
				   " WHERE id_operatore = ".$id_operatore.
				   " AND        id_comm = ".$id_comm.
		 		   " AND        id_rint = ".$id_rint.
		 		   " AND       tipo_att = ".$tipo_att;
		$objDB->exec($sSql);
	}
	// controlli
	foreach ($_REQUEST as $key => $value) {
		if (substr($key, 0, 6) != 'zzore_') {continue;}		
		list($pref,$id_comm,$id_rint,$tipo_att,$dtUnix)  = explode("_",$key); 		
		$ore = str2num($value);		
		if ($ore === false) {
			$msg .= 'inserito valore '.$value.' non numerico</br>';
			continue;
		}
	}
	if ($msg != '') {return $msg;}
	
	foreach ($_REQUEST as $key => $value) {
		if (substr($key, 0, 6) != 'zzore_') {continue;}		
		list($pref,$id_comm,$id_rint,$tipo_att,$dtUnix)  = explode("_",$key); 		
		$ore = str2num($value);		
		$data = date("Y-m-d",$dtUnix);
		$sSql = " SELECT id_att FROM attivita WHERE ".
					"          id_rint = ".$id_rint.
					" AND      id_comm = ".$id_comm.		
					" AND id_operatore = ".$id_operatore.
		 			" AND     tipo_att = ".$tipo_att.
					" AND         data = '".$data."'";
		//dbg_($sSql);
		$objDB->exec($sSql, 'sSelAtt');
		$id_att = 0;
		if ($acAtt = $objDB->fetch('sSelAtt')) {
			$id_att = $acAtt['id_att'];	
			while($acX = $objDB->fetch('sSelAtt')) { //cancella rec doppi (non dovrebbero esistere)
				$sSql = " DELETE FROM attivita WHERE id_att = ".$acX['id_att'];
				//dbg_($sSql);
				$objDB->exec($sSql);				
			}
		}
		if ($id_att > 0) {
			if ($ore == 0) {
				$sSql = " DELETE FROM attivita WHERE id_att = ".$id_att;
				//dbg_($sSql);
				$objDB->exec($sSql);				
			} else {
			   	$sSql = " UPDATE attivita SET ore  = ".$ore." ".
			   	        " WHERE id_att = ".$id_att;
			   //dbg_($sSql);
				$objDB->exec($sSql);		
			}
		} elseif ($ore != 0) {			
		   	$sSql = "INSERT INTO attivita SET ".
			        "       id_comm = ".$id_comm.
			        ",      id_rint = ".$id_rint.
			        ", id_operatore = ".$id_operatore.
			        ",     tipo_att = ".$tipo_att.
					",         data = '".$data."' ".	
					",         ore  = ".$ore." ";
		   //dbg_($sSql);
			$objDB->exec($sSql);		
		}	
	}
	return "*** aggiornamento effettuato ***";
}
?>